﻿$(document).ready(function () {
    if (parseInt(nPermissions) >= 3) {
        $("#btnsave").click(function () {
            if ($("#btnsave").val() == "Edit") {
                $('table[editcellcontent="true"]').InitialEditTableCell();
                $("#btnsave").val("Done");
                $(".changed").addClass("status");
            }
            else {
                setTimeout(function () {
                    $("#btnsave").val("Edit");
                    $(".changed").removeClass("status");
                    SaveChangeItems();
                }, 50);
            }
        });
    } else {
        $("#btnsave").remove();
    }
    $('*[zoomable="true"]').InitialZoom();
    //$('table[editcellcontent="true"]').SelectColumn('selectcolumn');
});

function BindbtnSave_Click() {


}

function SaveChangeItems() {
    var rowchanges = $('tr[changed="true"]');
    $('table[editcellcontent="true"]').DisableEditTableCell();
    if ($(rowchanges).length == 0) {
        return;
    }
    var ID = -1, Cont20 = "", Tons20 = "", Kms20 = "", Doprice20 = "", Note20 = "", Cont40 = "", Tons40 = "", Kms40 = "", Doprice40 = "", Note40 = "";

    var temp = "";
    var items = new Array();
    var changecells = new Array();
    var _cell;
    $.each(rowchanges, function (i, item) {
        ID = $(this).attr("truckerpriceid");

        _cell = $(this).children('td[code="Cont20"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            Cont20 = Encoder.htmlEncode(temp);
        } else {
            Cont20 = "";
        }
        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }

        //Tons20
        _cell = $(this).children('td[code="Tons20"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            Tons20 = Encoder.htmlEncode(temp);
        } else {
            Tons20 = "";
        }
        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }

        //Kms20
        _cell = $(this).children('td[code="Kms20"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            Kms20 = Encoder.htmlEncode(temp);
        } else {
            Kms20 = "";
        }
        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }
        //Doprice20
        _cell = $(this).children('td[code="DoPrice20"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            DoPrice20 = Encoder.htmlEncode(temp);
        } else {
            DoPrice20 = "";
        }
        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }
        //note20
        _cell = $(this).children('td[code="Note20"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            Note20 = Encoder.htmlEncode(temp);
        } else {
            Note20 = "";
        }
        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }
        //Conte 40
        _cell = $(this).children('td[code="Cont40"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            Cont40 = Encoder.htmlEncode(temp);
        } else {
            Cont40 = "";
        }
        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }
        //Tons40
        _cell = $(this).children('td[code="Tons40"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            Tons40 = Encoder.htmlEncode(temp);
        } else {
            Tons40 = "";
        }
        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }

        //Kms40
        _cell = $(this).children('td[code="Kms40"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            Kms40 = Encoder.htmlEncode(temp);
        } else {
            Kms40 = "";
        }
        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }
        //Doprice40
        _cell = $(this).children('td[code="DoPrice40"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            DoPrice40 = Encoder.htmlEncode(temp);
        } else {
            DoPrice40 = "";
        }
        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }
        //Note40
        _cell = $(this).children('td[code="Note40"]');
        temp = $.trim($(_cell).html());
        if (temp.toLowerCase() != "no check") {
            Note40 = Encoder.htmlEncode(temp);
        } else {
            Note40 = "";
        }
        if (typeof $(_cell).attr("value") != "undefined" && temp.toLowerCase() != "no check") {
            changecells.push(_cell);
        }
        items.push("{'acc':'TruckerPrice','ID':'" + ID + "','Cont20':'" + Cont20 + "','Tons20':'" + Tons20 + "','Kms20':'" + Kms20 +
                        "','DoPrice20':'" + DoPrice20 + "','Note20':'" + Note20 +
                        "','Cont40':'" + Cont40 + "','Tons40':'" + Tons40 + "','Kms40':'" + Kms40 +
                        "','DoPrice40':'" + DoPrice40 + "','Note40':'" + Note40 + "'}");
    });
    $.ajax({
        type: 'POST',
        url: "Handlers/UpdatePriceAreas.ashx?acc=TruckerPrice",
        data: { items: "[" + items.toString() + "]" },
        success: function (data) {
            if (data == "access denied!") {
            } else if (data == "not valid") {

            } else {
                $.each(changecells, function (i, item) {
                    $(item).addClass("has-value");
                    $(item).attr("title", data.UserName + ": " + data.Date);

                });

                var _newdate = data.Date.toString().replace(/-/g, '');
                $.each(rowchanges, function (i, item) {
                    $(item).children('*[code="update"]').text(_newdate);
                });
                $('tr[changed="true"]').removeAttr("changed");
            }
        },
        dataType: "json"
    });
}
